<?php
/**
 * @see ZendX_JQuery_View_Helper_UiWidget
 */
require_once "ZendX/JQuery/View/Helper/UiWidget.php";

/**
 * Помощник вида, реализующий выбор цвета
 * 
 * @category Phorm
 * @package JQuery_View_Helper
 * @name /Phorm/JQuery/View/Helper/MiniColorPicker.php
 * @copyright phorm.ru
 * @since 17.03.2012
 * @author vladimir@kachalov.net
 */
class Phorm_JQuery_View_Helper_MiniColorPicker extends ZendX_JQuery_View_Helper_UiWidget {
	/**
	 * Render a Color Picker in an FormText field.
	 *
	 * @param  string $id
	 * @param  string $value
	 * @param  array  $params
	 * @param  array  $attribs
	 * @return string
	 */
    public function miniColorPicker($id, $value='', array $params=array(), array $attribs=array()) {
	    
    	$attribs = $this->_prepareAttributes($id, $value, $attribs);

	    if(strlen($value) >= 6) {
	        $params['color'] = $value;
	    }

	    if(count($params) > 0) {
            $params = ZendX_JQuery::encodeJson($params);
	    } else {
	        $params = "{}";
	    }
	    
	    $this->jquery->addJavascriptFile($this->view->BaseUrl().'/ui/minicolors/jquery.miniColors.min.js');
		$this->jquery->addStylesheet($this->view->BaseUrl().'/ui/minicolors/jquery.miniColors.css');

        $js = sprintf('%s("#%s").miniColors(%s);',
            ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
            $attribs['id'],
            $params
        );

        $this->jquery->addOnLoad($js);

	    return $this->view->formText($id, $value, $attribs);
    }
}